package cn.tansanqinger.controller.chatroom;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import cn.tansanqinger.pojo.Chatmessage;
import cn.tansanqinger.util.DateUtil;
import cn.tansanqinger.util.Tools;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
@RestController
@RequestMapping("/chatroom")
public class MsgController {

    private final MsgSendService msgSendService;

    @RequestMapping("/send")
    @ResponseBody
    public String send(HttpServletRequest request) {
    	String fname = request.getParameter("fname");
    	String sname = request.getParameter("sname");
    	Chatmessage chatmessage = new Chatmessage();//添加数据
    	chatmessage.setCdate(DateUtil.getTime());
    	chatmessage.setCtime(System.currentTimeMillis()/1000);
    	chatmessage.setFaccount(fname);
    	chatmessage.setSaccount(sname);
    	chatmessage.setMessage(request.getParameter("msg"));
    	msgSendService.chatMessageSave(chatmessage);
        Map<String,Object> map=new HashMap<>();
        map.put("msg",request.getParameter("msg"));
        map.put("fname", fname);
        map.put("author", fname);
        map.put("date", DateUtil.getHMS());
        map.put("isme", 0);
        int size = msgSendService.sendMsg(map,sname,"server_event");
        map=new HashMap<>();
        map.put("msg",request.getParameter("msg"));
        map.put("fname", sname);
        map.put("author", fname);
        map.put("isme", 1);
        map.put("date", DateUtil.getHMS());
        msgSendService.sendMsg(map,fname,"server_event");
        return "发送成功" + size;
    }
    
    
    //请求之前的字段
    @RequestMapping("/beforeChatMessage")
    @ResponseBody
    public List<Chatmessage> beforeChatMessage(HttpServletRequest request){
    	String sname = Tools.isEmpty(request.getParameter("sname"));
    	String fname = Tools.isEmpty(request.getParameter("fname"));
    	return msgSendService.beforeChatMessage(sname,fname);
    }
}
